/*
    1.该文件是用于创建一个为Count组件服务的Redux reducer，reducer的本质就是一个函数
    2.reducer函数会接到两个参数：state和action，其中state是当前的状态，action是触发reducer的动作
*/
import { INCREMENT, DECREMENT } from '../actions/count';

const initialState = {
    count: 0,
};

export default function countReducer(state = initialState, action) {
    // 从action中获取type和data
    const { type, data } = action;
    if (type === INCREMENT) {
        return { ...state, count: state.count + data };
    } else if (type === DECREMENT) {
        return { ...state, count: state.count - data };
    } else {
        return state;
    }
}